package com.zhongyu.order.provider.apilmpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhongyu.comm.api.OrderApi;
import com.zhongyu.comm.pojo.Order;
import com.zhongyu.comm.pojo.OrderLinkUser;
import com.zhongyu.comm.pojo.Task;
import com.zhongyu.comm.util.JacksonUtil;
import com.zhongyu.comm.vo.CreateOrderMessage;
import com.zhongyu.order.provider.mapper.OrderLinkeUserMapper;
import com.zhongyu.order.provider.mapper.OrderMapper;
import com.zhongyu.order.provider.mapper.TaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@Slf4j
public class OrderApiImpl implements OrderApi {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderLinkeUserMapper orderLinkUserMapper;
    @Resource
    private TaskMapper taskMapper;

    @Override
    public Task findTaskById(String id) {
        return taskMapper.selectById(id);
    }

    @Override
    public int updateTryCountByTaskId(String id, LocalDateTime updateTime) {
        return taskMapper.updateTryCountByTaskId(id, updateTime);
    }

    @Override
    @Transactional
    public void processOrder(Task task) {
        String requestBody = task.getRequestBody();
        CreateOrderMessage message = (CreateOrderMessage) JacksonUtil.json2Obj(requestBody, CreateOrderMessage.class);
        Order order = new Order();
        BeanUtils.copyProperties(message, order);
        order.setTotalAmount(message.getAmount());
        order.setCreatedTime(LocalDateTime.now());
//        log.debug(order+"order:");
        orderMapper.insert(order);
//      log.debug(" orderMapper.insert(order);"+ orderMapper.insert(order));
        String linkIds = message.getLinkIds();
        String[] linkIdArr = linkIds.split(",");
        String seatPositions = message.getSeatPositions();
        String[] seatPositionArr = seatPositions.split(",");
//     log.debug("message.getOrderNo():"+message.getOrderNo());
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.eq("order_no", message.getOrderNo());
        int orderId = orderMapper.selectOne(queryWrapper).getId();
        String[] priceArr = message.getPerPrice().split(",");
        for (int i = 0; i < linkIdArr.length; i++) {
            String linkId = linkIdArr[i];
            String seatPosition = seatPositionArr[i];
            String[] xy = seatPosition.split("_");
            OrderLinkUser orderLinkUser = new OrderLinkUser();
            orderLinkUser.setCreatedTime(LocalDateTime.now());
            orderLinkUser.setLinkUserId(Integer.parseInt(linkId));
            orderLinkUser.setOrderId(orderId);
            orderLinkUser.setX(Integer.parseInt(xy[0]));
            orderLinkUser.setY(Integer.parseInt(xy[1]));
            orderLinkUser.setPrice(Double.parseDouble(priceArr[i]));
            orderLinkUserMapper.insert(orderLinkUser);
        }
        taskMapper.updateStatusByTaskId(task.getId(), 1, LocalDateTime.now());

    }

    @Override
    public int addTask(Task task) {
        return taskMapper.insert(task);
    }
}
